//商品列表
var app = getApp(); //全局APP

Page({
	//页面的初始数据
	data: {
		plan_class_id: 0,
		id: 0,
		listData: [],
		showData: [],
		studentList: [],
		studentId: [],
		studentIndex: 0,
		studentNum: 0,
		currYear: '',
		currSeason: '',
		currentXh: '',
		filterDlg: false
	},

	//加载执行
	onLoad: function(options) {
		var id = options.id;
		var plan_class_id = options.plan_class_id;
		this.setData({
			plan_class_id: plan_class_id,
			id: id
		});
		
		this.getBookAplyLogDetail();
	},
	
	//加载执行
	onShow: function() {
	},
	
	getBookAplyLogDetail: function() {
		var page = this;
		var data = {
			plan_class_id: page.data.plan_class_id,
			id: page.data.id
		};
		
		// 获取班级
		app.api.getBookAplyLogDetail(data, function(res) {
			if (res.status) {
				var tmp_data = [];
				
				var tmp_Name = [];
				var tmp_id = [];
				var studentNum = 0;
				
				if(res.studentList) {
					for (var i = 0; i < res.studentList.length; i++) {
						tmp_Name[i] = res.studentList[i].xh + ' ' + res.studentList[i].xm;
						tmp_id[i] = res.studentList[i].xh;
					}
					
					studentNum = res.studentList.length;
				}
				
				page.setData({
					listData: res.data,
					showData: res.data,
					currYear: res.currYear,
					currSeason: res.currSeason,
					studentList: tmp_Name,
					studentId: tmp_id,
					studentNum: studentNum
				});
			} else {
				//报错了
				app.common.errorToBack(res.msg, 0);
				page.setData({
					listData: [],
					showData: [],
					currYear: '',
					currSeason: '',
					studentList: []
				});
			}
		});
	},
	 
	inputTyping: function(e) {
		if (e.detail.value !== '') {
			var actionArr = this.data.listData;
			var val = e.detail.value;
			var temp = []; 
			
			actionArr.forEach(function(i) {
				var xm = i.xm;
				var xh = i.xh;
				if (xm.indexOf(val) != -1 || xh.indexOf(val) != -1) {
					temp.push(i);
				}
			});
			
			this.setData({
				showData: temp,
			})
		} else {
			this.setData({
				showData: this.data.listData,
			})
		}
	},
	
	// 隐藏搜索框样式
	hideInput: function() {
		this.setData({
			inputVal: "",
			inputShowed: false,
			showData: this.data.listData
		});
	},

	changeStudentShow: function(e) {
		this.setData({
			currentXh: e.currentTarget.dataset.xh
		})
		this.filter(e);
	},
	
	// 打开筛选窗口
	filter: function(e) {
		this.setData({
			filterDlg: true
		});
	},

	// 关闭筛选窗口
	close: function() {
		this.setData({
			filterDlg: false
		});
	},

	// 筛选提交
	filterSubmit: function() {
		var page = this;
			
		var data = {
			srcXh: page.data.currentXh,
			destXh: page.data.studentId[page.data.studentIndex],
			classId: page.data.plan_class_id
		};

		page.changeStudent(data);
		 
		this.setData({
			filterDlg: false
		});
	},
 
	changeStudent: function(data) {
		var page = this;
		
		app.api.changeStudent(data, function(res) {
			if (res.status) {
				app.common.successToShow(res.msg, function() {
					page.getBookAplyLogDetail();
				});
			} else {
				app.common.errorToBack(res.msg, 0);
			}
		});
	},
	 
	// 绑定学生
	bindstudentSelect: function(e) {
		var page = this;
		page.setData({
			studentIndex: e.detail.value
		}); 
	},
});
